﻿using System;
using Common.Enumerations;
using Newtonsoft.Json;
using Sanelib.DataView;
using Sanelib.Dto.Enumerations;

namespace Common.Views
{
    public class MemberView : AuditedView
    {
        public string TagId { get; set; }
        public string Code { get; set; }
        public DateTime? DateOfBirth { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Mobile { get; set; }
        public string ScreenMessage { get; set; }
        public DateTime? ValidFrom { get; set; }
        public DateTime? ValidTo { get; set; }
        public bool? SendSms { get; set; }
        public bool? SendEmail { get; set; }
        public bool? UsePrint { get; set; }
        public bool? Block { get; set; }

        public string PasswordHash { get; set; }
        public string PasswordSalt { get; set; }

        public int FailedAttemptCount { get; set; }

        [JsonIgnore]
        public Role Role { get; set; }
        public string RoleValue { get { return Role == null ? "" : Role.Value; } set { Role = Role.FromValue(value); } }
        public string RoleName { get { return Role == null ? "" : Role.DisplayName; } set { Role = Role.FromDisplay(value); } }
        
        [JsonIgnore]
        public UserStatus UserStatus { get; set; }
        public string UserStatusValue { get { return UserStatus == null ? "" : UserStatus.Value; } set { UserStatus = UserStatus.FromValue(value); } }
        public string UserStatusName { get { return UserStatus == null ? "" : UserStatus.DisplayName; } set { UserStatus = UserStatus.FromDisplay(value); } }
        
        public string PasswordRetrievalToken { get; set; }
        public DateTime? LastLoginTime { get; set; }
        public DateTime? PasswordRetrievalTokenExpirationDate { get; set; }
        public DateTime? LastPasswordChangedDate { get; set; }

        public string Session { get; set; }

        public bool IsAdmin()
        {
            return Code == "admin";
        }

        public bool HasPermission(Permission permission)
        {
            return permission.Roles.Contains(Role);
        }
    }
}